/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package edu.buaa.act.travel.m.tpdl;

import java.text.ParseException;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import edu.buaa.act.travel.m.utils.TimeUtil;

public abstract class TpdlActivity extends Activity implements JSONable{

	private static final long serialVersionUID = 1L;
	protected long dbid;
	protected String name;
	protected Date startTime;
	protected Date endTime;
	protected Double latitude;
	protected Double longitude;
	protected String address;
	
	protected JSONObject toJSONObject(){
		JSONObject o = new JSONObject();
		try {
			o.put("id", id);
			o.put("name", name);
			o.put("startTime", TimeUtil.date2String(startTime));
			o.put("endTime", TimeUtil.date2String(endTime));
			o.put("latitude", latitude);
			o.put("longitude", longitude);
			o.put("address", address);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return o;
	}
	
	public String toJSONString(){
		JSONObject o = toJSONObject();
		return o.toString();
	}
	
	public void fromJSON(JSONObject j) throws JSONException{
		if(j.has("id")){
			this.setId(j.getString("id"));
		}
		if(j.has("name")){
			this.setName(j.getString("name"));
		}
		try {
			if(j.has("startTime")){
				this.setStartTime(TimeUtil.string2Date(j.getString("startTime")));
			}
			if(j.has("endTime")){
				this.setEndTime(TimeUtil.string2Date(j.getString("endTime")));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(j.has("latitude")){
			this.setLatitude(j.getDouble("latitude"));
		}
		if(j.has("longitude")){
			this.setLongitude(j.getDouble("longitude"));
		}
		if(j.has("address")){
			this.setAddress(j.getString("address"));
		}
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public long getDbid() {
		return dbid;
	}

	public void setDbid(long dbid) {
		this.dbid = dbid;
	}

}
